package cn.tedu.straw.portal.service.impl;

import cn.tedu.straw.portal.dto.PostCommentDTO;
import cn.tedu.straw.portal.ex.AnswerNotFoundException;
import cn.tedu.straw.portal.ex.InsertException;
import cn.tedu.straw.portal.mapper.AnswerMapper;
import cn.tedu.straw.portal.mapper.CommentMapper;
import cn.tedu.straw.portal.model.Answer;
import cn.tedu.straw.portal.model.Comment;
import cn.tedu.straw.portal.model.Question;
import cn.tedu.straw.portal.service.IAnswerService;
import cn.tedu.straw.portal.service.ICommentService;
import cn.tedu.straw.portal.service.IQuestionService;
import cn.tedu.straw.portal.vo.AnswerVO;
import cn.tedu.straw.portal.vo.CommentVO;
import cn.tedu.straw.portal.vo.ResultVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2021-02-26
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    AnswerMapper answerMapper;
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    private IAnswerService answerService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IQuestionService questionService;

    @Override
    public List<CommentVO> findCommentsById(Integer QuestionId) {
        Answer answer =  answerMapper.findById(QuestionId);
        return commentMapper.findCommentsByAnswerId(answer.getId());
    }

   /* @Override
    public ResultVo findDetailsByQuestionId(Integer questionId) {
        ResultVo resultVo = new ResultVo();


        Question question = questionService.getById(questionId);
        resultVo.setQuestion(question);


        List<Answer> list = answerService.lambdaQuery().eq(Answer::getQuestionId, question).list();


        List<AnswerVO> answerVOS = list.stream().map(m -> {
            AnswerVO answerVO = new AnswerVO();
            BeanUtils.copyProperties(m, answerVO);


            List<Comment> comments = commentService.lambdaQuery().eq(Comment::getAnswerId, answerVO.getId()).list();

            List<CommentVO> commentVOS = comments.stream().map(c -> {
                CommentVO commentVO = new CommentVO();
                BeanUtils.copyProperties(c, commentVO);
                return commentVO;
            }).collect(Collectors.toList());
            answerVO.setCommentS(commentVOS);
            return answerVO;
        }).collect(Collectors.toList());
        resultVo.setAnswers(answerVOS);
        return resultVo;
    }*/

    @Override
    public void post(PostCommentDTO postCommentDTO, Integer userId, String userNickName) {
        //从参数postCommentDTO 中获取answerId
        Integer answerId = postCommentDTO.getAnswerId();
        //调用answerMapper.findById()查询”答案“
        Answer answer = answerMapper.findById(answerId);
        //判断查询结果是否为null
        if(answer == null){
            //是,抛出AnswerNotFoundException
            throw new AnswerNotFoundException("发表评论失败!答案数据不存在!");
        }

        //创建当前时间对象now
        LocalDateTime now=LocalDateTime.now();
        //创建Comment对象
        Comment comment = new Comment();
        //向Comment对象中补全数据: content < 参数postCommentDTO
        comment.setContent(postCommentDTO.getContent());
        //向Comment对象中补全数据: userId < 参数userId
        comment.setUserId(userId);
        //向Comment对象中补全数据: userNickName < 参数userNickName
        comment.setUserNickName(userNickName);
        //向Comment对象中补全数据: answerId < answerId
        comment.setAnswerId(answerId);
        //向Comment对象中补全数据: gmtCreate < now
        comment.setGmtCreate(now);
        //向Comment对象中补全数据: gmtModified < now
        comment.setGmtModified(now);
        //调用commentMapper.insert() 插入”评论“数据,并返回结果
        int rows = commentMapper.insert(comment);
        //判断返回结构是否不为1
        if(rows != 1){
            //是: 抛出InsertException
            throw new InsertException("发表评论失败!服务器忙,请稍后再试");
        }
    }
}
